package com.web;

import java.util.Date;

public class ProductCategoryManageAction extends ActionSupport{

	// 依赖注入逻辑组件
	protected IProductCategoryService productCategoryService;
	private Integer id;
	private ProductCategory productCategory;
	private String name;
	private String type;
	private String key;
	private Map<String, Object> resultMap = new HashMap<String, Object>();
	
	public void setProductCategoryService(IProductCategoryService productCategoryService) {
		this.productCategoryService = productCategoryService;
	}

	public String addProductCategory() throws Exception{
		resultMap = new HashMap<String, Object>();
		this.productCategoryService.addProductCategory(this.name);
		resultMap.put("status", true);
		resultMap.put("msg", "保存成功");
		return SUCCESS;
	}
	
	public String updateProductCategory() throws Exception{
		resultMap = new HashMap<String, Object>();
		ProductCategory productCategory = this.productCategoryService.getProductCategory(id);
		productCategory.setName(name);
		this.productCategoryService.updateProductCategory(productCategory);
		resultMap.put("status", true);
		resultMap.put("msg", "保存成功");
		return SUCCESS;
	}

	public String editProductCategory() throws Exception{
		if(id != null){
			return updateProductCategory();
		}
		return addProductCategory();
	}
	
	public String manageProductCategory() throws Exception{
		if(id != null){
			ActionContext ctx = ActionContext.getContext();
			ProductCategory productCategory = this.productCategoryService.getProductCategory(id);
			String name = productCategory.getName();
			Date addDate = course.getAddDate();
			
			ctx.put("id", id);
			ctx.put("name", name);
			ctx.put("addDate", addDate);
		}
		
		return "manageProductCategory";
	}

	public String listProductCategory() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		List<ProductCategory> list = productCategoryService.listProductCategory();
		
		ctx.put("list", list);
		return "listProductCategory";
	}
	
	public String delProductCategory() throws Exception{
		resultMap = new HashMap<String, Object>();
		productCategoryService.delProductCategory(id);
		resultMap.put("status", true);
		resultMap.put("msg", "删除成功");
		return SUCCESS;
	}
	
	public String productCategoryInfo() throws Exception{
		ProductCategory productCategory = productCategoryService.getProductCategory(id);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("productCategory", roductCategory);
		return "showProductCategory";
	}
	
//前端接口
	/*
	 * 列出所有产品类别列表
	 */
	public String outwardListProductCategory() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		List<ProductCategory> list = productCategoryService.listProductCategory();
		
		ctx.put("list", list);
		System.out.println(list);
		return "outwardListProductCategory";
	}	
	
	/*
	 * 根据关键词列出产品类别列表
	 * @param type
	 * @param key
	 */
	public String outwardFindProductCategory() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		List<ProductCategory> list;
		if(key == null){
			return "outwardFindProductCategory";
		}
		key = java.net.URLDecoder.decode(key, "utf-8");
		if(key == null){
			list = productCategoryService.listProductCategory();
		} else{
			list = productCategoryService.findProductCategory(key);
		}
		ctx.put("list", list);
		System.out.println(list);
		return "outwardFindProductCategory";
	}
	
	/*
	 * 课程信息
	 */
	public String showProductCategory() throws Exception{
		ProductCategory productCategory = productCategoryService.getProductCategory(id);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("productCategory", productCategory);
		return "showProductCategory";
	}
	/*---------------------------------------*/
	
	public ProductCategory getProductCategory() {
		return productCategory;
	}

	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

}

